Friend Class DBReportSchedule
    Inherits DBBase

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Load(ByRef oObject As ReportSchedule)
        Dim sSQL As String = "select"
        sSQL = sSQL & "     [serverid],"
        sSQL = sSQL & "     [databaseid],"
        sSQL = sSQL & "     [delaydays],"
        sSQL = sSQL & "     [IsActive],"
        sSQL = sSQL & "     [timeofday],"
        sSQL = sSQL & "     [lastreportcompiledate],"
        sSQL = sSQL & "     [recipientemailaddresses],"
        sSQL = sSQL & "     [lastreportsenddate],"
        sSQL = sSQL & "     [CreateName],"
        sSQL = sSQL & "     [CreateDate],"
        sSQL = sSQL & "     [ChangeName],"
        sSQL = sSQL & "     [ChangeDate]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     [ReportSchedule]"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        If oData.Rows.Count > 0 Then
            With oObject
                .ServerID = NullToNumber(oData.Rows(0).Item("serverid"))
                .DatabaseID = NullToNumber(oData.Rows(0).Item("databaseid"))
                .DelayDays = oData.Rows(0).Item("delaydays")
                .IsActive = oData.Rows(0).Item("IsActive")
                .TimeOfDay = oData.Rows(0).Item("timeofday")
                .LastReportCompileDate = NullToString(oData.Rows(0).Item("lastreportcompiledate"))
                .RecipientEmailAddresses = NullToString(oData.Rows(0).Item("recipientemailaddresses"))
                .LastReportSendDate = NullToString(oData.Rows(0).Item("lastreportsenddate"))
                .CreateName = oData.Rows(0).Item("CreateName")
                .CreateDate = oData.Rows(0).Item("CreateDate")
                .ChangeName = NullToString(oData.Rows(0).Item("ChangeName"))
                .ChangeDate = NullToString(oData.Rows(0).Item("ChangeDate"))
            End With
        Else
            oObject._ObjectExists = False
        End If

        oData.Dispose()
    End Sub

    Public Sub Update(ByRef oObject As ReportSchedule)
        Dim sSQL As String = "update [ReportSchedule] set"
        sSQL = sSQL & "     [serverid]=" & ValueToDBValue(oObject.ServerID) & ","
        sSQL = sSQL & "     [DatabaseID]=" & ValueToDBValue(oObject.DatabaseID) & ","
        sSQL = sSQL & "     [DelayDays]=" & ValueToDBValue(oObject.DelayDays) & ","
        sSQL = sSQL & "     [IsActive]=" & BooleanToBit(oObject.IsActive) & ","
        sSQL = sSQL & "     [TimeOfDay]=" & ValueToDBValue(oObject.TimeOfDay) & ","
        sSQL = sSQL & "     [lastreportcompiledate]=" & ValueToDBValue(oObject.LastReportCompileDate) & ","
        sSQL = sSQL & "     [RecipientEmailAddresses]=" & ValueToDBValue(oObject.RecipientEmailAddresses) & ","
        sSQL = sSQL & "     [lastreportsenddate]=" & ValueToDBValue(oObject.LastReportSendDate) & ","
        sSQL = sSQL & "     [CreateName]=" & ValueToDBValue(oObject.CreateName) & ","
        sSQL = sSQL & "     [CreateDate]=" & ValueToDBValue(oObject.CreateDate) & ","
        sSQL = sSQL & "     [ChangeName]=" & ValueToDBValue(oObject.ChangeName) & ","
        sSQL = sSQL & "     [ChangeDate]=" & ValueToDBValue(oObject.ChangeDate)
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub

    Public Sub Add(ByRef oObject As ReportSchedule)
        Dim bIsSaved As Boolean = False

        Do Until bIsSaved = True
            Dim sSQL As String = "insert into [ReportSchedule] ("
            sSQL = sSQL & "     [ID],"
            sSQL = sSQL & "     [serverid],"
            sSQL = sSQL & "     [DatabaseID],"
            sSQL = sSQL & "     [DelayDays],"
            sSQL = sSQL & "     [IsActive],"
            sSQL = sSQL & "     [TimeOfDay],"
            sSQL = sSQL & "     [RecipientEmailAddresses],"
            sSQL = sSQL & "     [lastreportcompiledate],"
            sSQL = sSQL & "     [lastreportsenddate],"
            sSQL = sSQL & "     [CreateName],"
            sSQL = sSQL & "     [CreateDate],"
            sSQL = sSQL & "     [ChangeName],"
            sSQL = sSQL & "     [ChangeDate]) values ("

            sSQL = sSQL & ValueToDBValue(oObject.ID) & ","
            sSQL = sSQL & ValueToDBValue(oObject.ServerID) & ","
            sSQL = sSQL & ValueToDBValue(oObject.DatabaseID) & ","
            sSQL = sSQL & ValueToDBValue(oObject.DelayDays) & ","
            sSQL = sSQL & BooleanToBit(oObject.IsActive) & ","
            sSQL = sSQL & ValueToDBValue(oObject.TimeOfDay) & ","
            sSQL = sSQL & ValueToDBValue(oObject.RecipientEmailAddresses) & ","
            sSQL = sSQL & ValueToDBValue(oObject.LastReportCompileDate) & ","
            sSQL = sSQL & ValueToDBValue(oObject.LastReportSendDate) & ","
            sSQL = sSQL & ValueToDBValue(oObject.CreateName) & ","
            sSQL = sSQL & ValueToDBValue(oObject.CreateDate) & ","
            sSQL = sSQL & ValueToDBValue(oObject.ChangeName) & ","
            sSQL = sSQL & ValueToDBValue(oObject.ChangeDate)
            sSQL = sSQL & ")"

            Try
                MyBase.ExecuteNonQuery(sSQL)

                bIsSaved = True

            Catch oEX As System.Data.SqlClient.SqlException
                If InStr(oEX.Message, "Violation of PRIMARY KEY constraint") > 0 Then
                    '---This ID is already in use.
                    oObject._New()
                Else
                    Throw oEX
                End If
            End Try
        Loop
    End Sub

    Public Sub Delete(ByRef oObject As ReportSchedule)
        Dim sSQL As String = "delete from [ReportSchedule]"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub
End Class
